using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;

namespace DotNetX.UI.Web
{
    /// <summary>
    /// This gets values from a form and validates them
    /// </summary>
    public class FormChecker
    {
        #region Properties

        /// <summary>
        /// The HTTP Request of the page
        /// </summary>
        public HttpRequest HttpRequest
        {
            get
            {
                // return the request
                return _httpRequest;
            }
            set
            {
                // set the request
                this._httpRequest = value;
            }
        }
        private HttpRequest _httpRequest = null;

        /// <summary>
        /// Whether the checker should sanitize any HTML from passed in controls (for HTML attacks)
        /// </summary>
        public bool SanitizeHTML
        {
            get
            {
                // return the value
                return this._sanitizeHTML;
            }
            set
            {
                // set the value
                this._sanitizeHTML = value;
            }
        }
        private bool _sanitizeHTML = false;

        /// <summary>
        /// Whether to checker should sanitize any SQL from passed in controls (for SQL Injection attacks)
        /// </summary>
        public bool SanitizeSQL
        {
            get
            {
                // return the value
                return this._sanitizeSQL;
            }
            set
            {
                // set the value
                this._sanitizeSQL = value;
            }
        }
        private bool _sanitizeSQL = false;

        /// <summary>
        /// This returns all of the error messages seperated by a <br /> tag
        /// </summary>
        public string ErrorMessagesByBrTag
        {
            get
            {
                // return the messages
                return this.GetErrorMessages("<br />");
            }
        }

        /// <summary>
        /// This value is true if this checker has any errors
        /// </summary>
        public bool HasErrors
        {
            get
            {
                // does this checker have any errors?
                return this._errorMessages.Count > 0;
            }
        }

        /// <summary>
        /// The error messages
        /// </summary>
        public List<string> ErrorMessages
        {
            get
            {
                // return the error messages
                return this._errorMessages;
            }
        }
        private List<string> _errorMessages = new List<string>();

        #endregion

        #region Constructors

        public FormChecker()
        {
        }
        public FormChecker(HttpRequest httpRequest)
        {
            if (httpRequest == null)
                throw new ArgumentNullException("'httpRequest' is null.");

            // set the request
            this._httpRequest = httpRequest;
        }
        public FormChecker(bool sanitizeSQL, bool sanitizeHTML)
        {
            // set the values
            this._sanitizeSQL = sanitizeSQL;
            this._sanitizeHTML = sanitizeHTML;
        }
        public FormChecker(bool sanitizeSQL, bool sanitizeHTML, HttpRequest httpRequest)
        {
            if (httpRequest == null)
                throw new ArgumentNullException("'httpRequest' is null.");

            // set the values
            this._httpRequest = httpRequest;
            this._sanitizeSQL = sanitizeSQL;
            this._sanitizeHTML = sanitizeHTML;
        }

        #endregion

        #region Sanitization

        /// <summary>
        /// This performs SQL sanitization on the passed string
        /// </summary>
        /// <param name="value">The string to sanitize</param>
        /// <returns>The sanitized string</returns>
        public string PerformSQLSanitization(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            
            // do we need to sanitize?
            if (value.Length == 0)
                return string.Empty;

            // sanitize
            // TODO: Perform good sanitization techniques here...
            return value.Replace('\'', ' ');
        }

        /// <summary>
        /// This performs HTML sanitization on the passed string
        /// </summary>
        /// <param name="value">The string to sanitize</param>
        /// <returns>The sanitized string</returns>
        public string PerformHTMLSanitization(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");

            // do we need to sanitize?
            if (value.Length == 0)
                return string.Empty;

            // sanitize
            // TODO: Perform good sanitization techniques here...
            string sanitizedString = value.Replace("<", "&lt;");
            sanitizedString = sanitizedString.Replace(">", "&gt;");
            return sanitizedString;
        }

        #endregion

        #region Get String Value

        /// <summary>
        /// This returns the value from the passed textbox as a string
        /// </summary>
        /// <param name="text">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <returns>The string value from the passed TextBox if valid. If not valid, this method returns String.Empty</returns>
        public string GetStringValue(TextBox textBox, string friendlyName)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // check the string value
            return this.CheckStringValue(textBox.Text, friendlyName);
        }

        /// <summary>
        /// This returns the value from the passed textBox as a string with no error checking
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <returns>The string value from the passed TextBox</returns>
        /// <remarks>This method performs no error checking, but will sanitize the passed string if this object is set to</remarks>
        public string GetStringValue(TextBox textBox)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");

            // check the string
            return this.CheckStringValue(textBox.Text, string.Empty, false);
        }

        /// <summary>
        /// This returns the value from the passed textbox in a TemplateWizardStep as a string
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <returns>The string value from the passed TextBox if valid. If not valid, this method returns String.Empty</returns>
        public string GetStringValue(TemplatedWizardStep step, string textBoxName, string friendlyName)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // check the string value
            return this.CheckStringValue(textBox.Text, friendlyName);
        }

        /// <summary>
        /// This returns the value from the passed textbox in a TemplateWizardStep as a string with no error checking
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <returns>The string value from the passed TextBox</returns>
        /// <remarks>This method performs no error checking, but will sanitize the passed string if this object is set to</remarks>
        public string GetStringValue(TemplatedWizardStep step, string textBoxName)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // check the string
            return this.CheckStringValue(textBox.Text, string.Empty, false);
        }

        /// <summary>
        /// This returns the value from the passed form based input control as a string
        /// </summary>
        /// <param name="formName">The name of the control on the form to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <returns>The string value from the control if valid. If not valid this method returns String.Empty</returns>
        public string GetStringValue(string formName, string friendlyName)
        {
            if (formName == null)
                throw new ArgumentNullException("'formName' is null.");
            if (formName.Length == 0)
                throw new ArgumentOutOfRangeException("'formName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // make sure we have a request
            if (this.HttpRequest == null)
                throw new NullReferenceException("'this.HttpRequest' is null.");

            // get the value of the control
            string value = this.HttpRequest.Form[formName];

            // check the string
            return this.CheckStringValue(value, friendlyName);
        }

        /// <summary>
        /// This returns a string and makes sure it is the correct length
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <param name="maxLength">The maximum length of the string</param>
        /// <returns>The string value from the passed TextBox if valid. If not valid, this method will return a truncated string with length = maxLength</returns>
        public string GetStringValueWithMaxLength(TemplatedWizardStep step, string textBoxName, string friendlyName, int maxLength)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // check the string
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // is the string the correct length?
            return this.CheckStringLength(value, friendlyName, maxLength);
        }

        /// <summary>
        /// This returns a string and makes sure it is the correct length
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <param name="maxLength">The maximum length of the string</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <returns>The string values from the passed TextBox. If not valid, this method will return a truncated string with length = maxLength</returns>
        /// <remarks>If isRequired = false, a length check will take place, but only if a string is there</remarks>
        public string GetStringValueWithMaxLength(TemplatedWizardStep step, string textBoxName, string friendlyName, int maxLength, bool isRequired)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // check the string
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // is the string the correct length?
            return this.CheckStringLength(value, friendlyName, maxLength);
        }

        /// <summary>
        /// This returns a string and makes sure it is the correct length
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <param name="maxLength">The maximum length of the string</param>
        /// <returns>The string value from the passed TextBox if valid. If not valid, this method will return a truncated string with length = maxLength</returns>
        public string GetStringValueWithMaxLength(TextBox textBox, string friendlyName, int maxLength)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // check the string
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // is the string the correct length?
            return this.CheckStringLength(value, friendlyName, maxLength);
        }

        /// <summary>
        /// This returns a string and makes sure it is the correct length
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <param name="maxLength">The maximum length of the string</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <returns>The string values from the passed TextBox. If not valid, this method will return a truncated string with length = maxLength</returns>
        /// <remarks>If isRequired = false, a length check will take place, but only if a string is there</remarks>
        public string GetStringValueWithMaxLength(TextBox textBox, string friendlyName, int maxLength, bool isRequired)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // check the string
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // is the string the correct length?
            return this.CheckStringLength(value, friendlyName, maxLength);
        }

        /// <summary>
        /// This checks the passed string's length. If it is too long, an StringToLong error is added
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        private string CheckStringLength(string value, string friendlyName, int maxLength)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            // value can be zero-length
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // trim the string
            value = value.Trim();

            // make sure the string is of the correct length
            if (value.Length > maxLength)
            {
                // add an error
                this.AddStringToLongError(friendlyName, maxLength);

                // return the truncated string
                return value.Substring(0, maxLength);
            }

            // return the valid string
            return value;
        }

        /// <summary>
        /// This checks a string value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <returns></returns>
        private string CheckStringValue(string value, string friendlyName, bool isRequired)
        {
            // trim the string
            value = value.Trim();

            // is the string required?
            if (isRequired)
            {
                // does this string contain a value?
                if (string.IsNullOrEmpty(value))
                {
                    // add an error
                    this.AddEmptyValueError(friendlyName);

                    // return no value
                    return string.Empty;
                }
            }

            // do we need to check for SQL injection attacks?
            if (this.SanitizeSQL)
                value = this.PerformSQLSanitization(value);

            // do we need to check for HTML attacks?
            if (this.SanitizeHTML)
                value = this.PerformHTMLSanitization(value);

            // return the value
            return value;
        }

        /// <summary>
        /// This checks a string value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <returns></returns>
        private string CheckStringValue(string value, string friendlyName)
        {
            // check the string
            return this.CheckStringValue(value, friendlyName, true);
        }

        #endregion

        #region Get E-Mail Value

        /// <summary>
        /// This returns the value as an E-Mail address from the passed textbox if it is valid
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <returns>The E-Mail address from the passed TextBox if it is valid, else String.Empty</returns>
        public string GetEMailValue(TemplatedWizardStep step, string textBoxName, string friendlyName)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // a valid E-Mail address?
            return this.CheckEMailValue(textBox.Text, friendlyName);
        }

        /// <summary>
        /// This returns the value as an E-Mail address from the passed textbox if it is valid
        /// </summary>
        /// <param name="text">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <returns>The E-Mail address from the passed TextBox if it is valid, else String.Empty</returns>
        public string GetEMailValue(TextBox textBox, string friendlyName)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // a valid E-Mail address?
            return this.CheckEMailValue(textBox.Text, friendlyName);
        }

        /// <summary>
        /// This returns the value as an E-Mail address from the passed form based input control as a string
        /// </summary>
        /// <param name="formName">The name of the control on the form to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <returns>The string value from the control if valid. If not valid this method returns String.Empty</returns>
        public string GetEMailValue(string formName, string friendlyName)
        {
            if (formName == null)
                throw new ArgumentNullException("'formName' is null.");
            if (formName.Length == 0)
                throw new ArgumentOutOfRangeException("'formName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // make sure we have a request
            if (this.HttpRequest == null)
                throw new NullReferenceException("'this.HttpRequest' is null.");

            // get the value of the control
            string value = this.HttpRequest.Form[formName];

            // a valid E-Mail address?
            return this.CheckEMailValue(value, friendlyName);
        }

        /// <summary>
        /// This checks the passed value to see if it is a valid E-Mail address
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <returns></returns>
        private string CheckEMailValue(string value, string friendlyName)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            // value can be zero-length here
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // is the string valid?
            value = this.CheckStringValue(value, friendlyName);
            if (value.Length == 0)
                return string.Empty;

            // is the textbox value a valid E-Mail address
            if (!StringType.IsEMailAddress(value))
            {
                // add an error
                this.AddBadEMailAddressError(value, friendlyName);

                // return no value
                return string.Empty;
            }

            // return the E-Mail address
            return value;
        }

        #endregion

        #region Get Phone Number Value

        /// <summary>
        /// This returns a country dependant phone from the passed textbox
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetPhoneNumberValue(TemplatedWizardStep step, string textBoxName, string friendlyName, string country)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException("'textBox' is null.");

            // check the phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // check the phone number
            return this.CheckPhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant phone from the passed textbox
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetPhoneNumberValue(TemplatedWizardStep step, string textBoxName, string friendlyName, string country, bool isRequired)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException("'textBox' is null.");

            // check the phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // check the phone number
            return this.CheckPhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant phone from the passed textbox
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetPhoneNumberValue(TextBox textBox, string friendlyName, string country)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // check the phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // check the phone number
            return this.CheckPhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant phone from the passed textbox
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetPhoneNumberValue(TextBox textBox, string friendlyName, string country, bool isRequired)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // check the phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // check the phone number
            return this.CheckPhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This checks to see if the passed phone number is valid
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <param name="country"></param>
        /// <returns></returns>
        private string CheckPhoneNumberValue(string value, string friendlyName, string country)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            // value can be zero-length
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // do we have a phone number?
            if (value.Length == 0)
                return string.Empty;

            // validate the phone number
            bool isValid = Country.ValidatePhoneNumber(value, country);

            // is the phone number valid?
            if (!isValid)
            {
                // add an error
                this.AddInvalidPhoneNumberError(value, friendlyName, country);
                return string.Empty;
            }

            // return the valid phone number
            return value;
        }

        #endregion

        #region Get Skype Phone Number Value

        /// <summary>
        /// This returns a country dependant skype phone from the passed textbox
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The skype phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetSkypePhoneNumberValue(TemplatedWizardStep step, string textBoxName, string friendlyName, string country)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException("'textBox' is null.");

            // check the skype phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // check the skype phone number
            return this.CheckSkypePhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant skype phone from the passed textbox
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetSkypePhoneNumberValue(TemplatedWizardStep step, string textBoxName, string friendlyName, string country, bool isRequired)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException("'textBox' is null.");

            // check the skype phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // check the skype phone number
            return this.CheckSkypePhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant skype phone from the passed textbox
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The skype phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetSkypePhoneNumberValue(TextBox textBox, string friendlyName, string country)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // check the skype phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // check the skype phone number
            return this.CheckSkypePhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant skype phone from the passed textbox
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <param name="country">The country the phone number is for</param>
        /// <returns>The phone number from the passed TextBox if valid, else String.Empty</returns>
        public string GetSkypePhoneNumberValue(TextBox textBox, string friendlyName, string country, bool isRequired)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // check the skype phone number
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // check the skype phone number
            return this.CheckSkypePhoneNumberValue(value, friendlyName, country);
        }

        /// <summary>
        /// This checks to see if the passed Skype phone number is valid
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <param name="country"></param>
        /// <returns></returns>
        private string CheckSkypePhoneNumberValue(string value, string friendlyName, string country)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            // value can be zero-length
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // do we have a phone number?
            if (value.Length == 0)
                return string.Empty;

            // validate the phone number
            bool isValid = Country.ValidateSkypePhoneNumber(value, country);

            // is the phone number valid?
            if (!isValid)
            {
                // add an error
                this.AddInvalidSkypePhoneNumberError(value, friendlyName, country);
                return string.Empty;
            }

            // return the valid phone number
            return value;
        }

        #endregion

        #region Get Postal Code Value

        /// <summary>
        /// This returns a country dependant postal code from the passed textbox
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="country">The country the postal code is for</param>
        /// <returns>The postal code from the passed TextBox if valid, else String.Empty</returns>
        public string GetPostalCodeValue(TemplatedWizardStep step, string textBoxName, string friendlyName, string country)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // check the postal code
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // check the postal code
            return this.CheckPostalCodeValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant postal code from the passed textbox
        /// </summary>
        /// <param name="step">The wizard step to get the textbox from</param>
        /// <param name="textBoxName">The name textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <param name="country">The country the postal code is for</param>
        /// <returns>The postal code from the passed TextBox if valid, else String.Empty</returns>
        public string GetPostalCodeValue(TemplatedWizardStep step, string textBoxName, string friendlyName, string country, bool isRequired)
        {
            if (step == null)
                throw new ArgumentNullException("'step' is null.");
            if (textBoxName == null)
                throw new ArgumentNullException("'textBoxName' is null.");
            if (textBoxName.Length == 0)
                throw new ArgumentOutOfRangeException("'textBoxName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // get the textbox
            TextBox textBox = step.ContentTemplateContainer.FindControl(textBoxName) as TextBox;
            if (textBox == null)
                throw new NullReferenceException(string.Format("'textBox' is null. The textbox '{0}' could not be found.", textBoxName));

            // check the postal code
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // check the postal code
            return this.CheckPostalCodeValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant postal code from the passed textbox
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="country">The country the postal code is for</param>
        /// <returns>The postal code from the passed TextBox if valid, else String.Empty</returns>
        public string GetPostalCodeValue(TextBox textBox, string friendlyName, string country)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // check the postal code
            string value = this.CheckStringValue(textBox.Text, friendlyName);

            // check the postal code
            return this.CheckPostalCodeValue(value, friendlyName, country);
        }

        /// <summary>
        /// This returns a country dependant postal code from the passed textbox
        /// </summary>
        /// <param name="textBox">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <param name="isRequired">Whether a string is required at all</param>
        /// <param name="country">The country the postal code is for</param>
        /// <returns>The postal code from the passed TextBox if valid, else String.Empty</returns>
        public string GetPostalCodeValue(TextBox textBox, string friendlyName, string country, bool isRequired)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // check the postal code
            string value = this.CheckStringValue(textBox.Text, friendlyName, isRequired);

            // check the postal code
            return this.CheckPostalCodeValue(value, friendlyName, country);
        }

        /// <summary>
        /// This checks to see if the passed postal code is valid
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <param name="country"></param>
        /// <returns></returns>
        private string CheckPostalCodeValue(string value, string friendlyName, string country)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            // value can be zero-length
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // do we have a postal code?
            if (value.Length == 0)
                return string.Empty;

            // validate the postal code
            bool isValid = Country.ValidatePostalCode(value, country);

            // is the postal code valid?
            if (!isValid)
            {
                // add an error
                this.AddInvalidPostalCodeError(value, friendlyName, country);
                return string.Empty;
            }

            // return the valid postal code
            return value;
        }

        #endregion

        #region Get Guid Value

        /// <summary>
        /// This returns the value as a Guid address from the passed textbox if it is valid
        /// </summary>
        /// <param name="text">The textbox to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the textbox in the error messages</param>
        /// <returns>The Guid from the passed TextBox if it is valid, else Guid.Empty</returns>
        public Guid GetGuidValue(TextBox textBox, string friendlyName)
        {
            if (textBox == null)
                throw new ArgumentNullException("'textBox' is null.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // a valid Guid?
            return this.CheckGuidValue(textBox.Text, friendlyName);
        }

        /// <summary>
        /// This returns the value as a Guid address from the passed form based input control as a string
        /// </summary>
        /// <param name="formName">The name of the control on the form to get the value from</param>
        /// <param name="friendlyName">The friendly name to call the control in the error messages</param>
        /// <returns>The Guid value from the control if valid. If not valid this method returns Guid.Empty</returns>
        public Guid GetGuidValue(string formName, string friendlyName)
        {
            if (formName == null)
                throw new ArgumentNullException("'formName' is null.");
            if (formName.Length == 0)
                throw new ArgumentOutOfRangeException("'formName' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // make sure we have a request
            if (this.HttpRequest == null)
                throw new NullReferenceException("'this.HttpRequest' is null.");

            // get the value of the control
            string value = this.HttpRequest.Form[formName];

            // a valid Guid?
            return this.CheckGuidValue(value, friendlyName);
        }

        /// <summary>
        /// This checks the passed value to see if it is a valid Guid
        /// </summary>
        /// <param name="value"></param>
        /// <param name="friendlyName"></param>
        /// <returns></returns>
        private Guid CheckGuidValue(string value, string friendlyName)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            // value can be zero-length here
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // is the string valid?
            value = this.CheckStringValue(value, friendlyName);
            if (value.Length == 0)
                return Guid.Empty;

            // is the textbox value a valid Guid?
            if (!StringType.IsGuid(value))
            {
                // add an error
                this.AddBadGuidError(value, friendlyName);

                // return no value
                return Guid.Empty;
            }

            // return the Guid
            return new Guid(value);
        }

        #endregion

        #region Add Errors

        /// <summary>
        /// This adds an error for a badly formatted E-Mail address
        /// </summary>
        /// <param name="badEMail">The badly formatted E-Mail address</param>
        /// <param name="friendlyName">The friendly name to use for the error message to represent the field</param>
        private void AddBadEMailAddressError(string badEMail, string friendlyName)
        {
            if (badEMail == null)
                throw new ArgumentNullException("'badEMail' is null.");
            // badEMail can be zero-length
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // is the E-Mail zero-length?
            if (badEMail.Length == 0)
                this._errorMessages.Add(string.Format("The E-Mail address for '{0}' cannot be empty.", friendlyName));
            else
                this._errorMessages.Add(string.Format("The E-Mail address '{0}' for '{1}' is not a valid E-Mail address. Please try again.", badEMail, friendlyName));
        }

        /// <summary>
        /// This adds an error to this checker
        /// </summary>
        /// <param name="friendlyName">The friendly name to use for the error message to represent the field</param>
        private void AddEmptyValueError(string friendlyName)
        {
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // add an error
            this._errorMessages.Add(string.Format("Please enter a value for '{0}'.", friendlyName));
        }

        /// <summary>
        /// This adds an error to this checker
        /// </summary>
        /// <param name="friendlyName"></param>
        /// <param name="maxLength"></param>
        private void AddStringToLongError(string friendlyName, int maxLength)
        {
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // add an error
            if(maxLength == 1)
                this._errorMessages.Add(string.Format("The value for '{0}' is to long. Please make sure that the value is a maximum of {1} character", friendlyName, maxLength));
            else
                this._errorMessages.Add(string.Format("The value for '{0}' is to long. Please make sure that the value is a maximum of {1} characters", friendlyName, maxLength));
        }

        /// <summary>
        /// This adds an error to this checker
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="friendlyName"></param>
        /// <param name="country"></param>
        private void AddInvalidPhoneNumberError(string phoneNumber, string friendlyName, string country)
        {
            if (phoneNumber == null)
                throw new ArgumentNullException("'phoneNumber' is null.");
            if (phoneNumber.Length == 0)
                throw new ArgumentOutOfRangeException("'phoneNumber' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // add the error
            this._errorMessages.Add(string.Format("The phone number '{0}' for '{1}' is not valid for the country '{2}'.",
                phoneNumber, friendlyName, country));
        }

        /// <summary>
        /// This adds an error to this checker
        /// </summary>
        /// <param name="skypePhoneNumber"></param>
        /// <param name="friendlyName"></param>
        /// <param name="country"></param>
        private void AddInvalidSkypePhoneNumberError(string skypePhoneNumber, string friendlyName, string country)
        {
            if (skypePhoneNumber == null)
                throw new ArgumentNullException("'skypePhoneNumber' is null.");
            if (skypePhoneNumber.Length == 0)
                throw new ArgumentOutOfRangeException("'skypePhoneNumber' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // add the error
            this._errorMessages.Add(string.Format("The Skype phone number '{0}' for '{1}' is not valid for the country '{2}'.",
                skypePhoneNumber, friendlyName, country));
        }

        /// <summary>
        /// This adds an error to this checker
        /// </summary>
        /// <param name="skypePhoneNumber"></param>
        /// <param name="friendlyName"></param>
        /// <param name="country"></param>
        private void AddInvalidPostalCodeError(string postalCode, string friendlyName, string country)
        {
            if (postalCode == null)
                throw new ArgumentNullException("'postalCode' is null.");
            if (postalCode.Length == 0)
                throw new ArgumentOutOfRangeException("'postalCode' is zero-length.");
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");
            if (country == null)
                throw new ArgumentNullException("'country' is null.");
            if (country.Length == 0)
                throw new ArgumentOutOfRangeException("'country' is zero-length.");

            // add the error
            this._errorMessages.Add(string.Format("The Postal Code/ZIP Number '{0}' for '{1}' is not valid for the country '{2}'.",
                postalCode, friendlyName, country));
        }

        /// <summary>
        /// This adds an error for a badly formatted Guid
        /// </summary>
        /// <param name="badGuid">The badly formatted Guid</param>
        /// <param name="friendlyName">The friendly name to use for the error message to represent the field</param>
        private void AddBadGuidError(string badGuid, string friendlyName)
        {
            if (badGuid == null)
                throw new ArgumentNullException("'badGuid' is null.");
            // badGuid can be zero-length
            if (friendlyName == null)
                throw new ArgumentNullException("'friendlyName' is null.");
            if (friendlyName.Length == 0)
                throw new ArgumentOutOfRangeException("'friendlyName' is zero-length.");

            // is the badGuid zero-length?
            if (badGuid.Length == 0)
                this._errorMessages.Add(string.Format("The Guid for '{0}' cannot be empty.", friendlyName));
            else
                this._errorMessages.Add(string.Format("The Guid '{0}' for '{1}' is not a valid Guid. Please try again.", badGuid, friendlyName));
        }

        #endregion

        /// <summary>
        /// This returns the error messages with the passed deliminator seperating them
        /// </summary>
        /// <param name="deliminator"></param>
        /// <returns></returns>
        public string GetErrorMessages(string deliminator)
        {
            if (deliminator == null)
                throw new ArgumentNullException("'deliminator' is null.");

            // build the message
            StringBuilder builder = new StringBuilder();
            foreach (string message in this.ErrorMessages)
            {
                // add the message to the list
                builder.AppendFormat("{0}{1}", message, deliminator);
            }

            // return the list
            return builder.ToString();
        }
    }
}
